package com.love_weinuo.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.love_weinuo.R;
import com.love_weinuo.bean.Constants;
import com.love_weinuo.util.LogUtil;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{
    private IWXAPI api;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        LogUtil.e("微信方面电报1：");
    }

    @Override
    public void onResp(BaseResp resp) {
        LogUtil.e("微信方面电报："+ resp.errCode);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (resp.errCode == -1) {
                Toast.makeText(WXPayEntryActivity.this, "支付失败，请确认您的手机微信登录正常", Toast.LENGTH_SHORT).show();
                finishPayAct();
            } else if (resp.errCode == -2) {
                Toast.makeText(WXPayEntryActivity.this, "取消支付", Toast.LENGTH_SHORT).show();
                finishPayAct();
            } else if (resp.errCode == 0) {
                Toast.makeText(WXPayEntryActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                finishPayAct();
            }
        }
    }
    /**
     * 做一个方法返回开始支付的界面，并关闭它
     */
    public void finishPayAct(){
        finish();
    }
}